import java.util.ArrayList;
import java.util.List;

interface Component{
    double GetPrice();
    StringBuilder GetName();
}

final class Part implements Component{
    private final double price;
    private final String name;
    public Part(String name, double price){
        this.name = name;
        this.price = price;
    }

    @Override
    public StringBuilder GetName() {
        return new StringBuilder(name);
    }

    @Override
    public double GetPrice() {
        return price;
    }
}
class Composite implements Component{
    private final String name;
    private List<Component> children = new ArrayList<>();

    public Composite(String name) {
        this.name = name;
    }
    public void add(Component component){
        children.add(component);
    }
    @Override
    public StringBuilder GetName() {
        StringBuilder children_names = new StringBuilder(name+"组件由：[ ");
        for(Component child:children){
            children_names.append(child.GetName()+" ");
        }
        children_names.append("]，组成");
        return children_names;
    }

    @Override
    public double GetPrice() {
        double price = 0d;
        for(var child : children){
            price += child.GetPrice();
        }
        return price;
    }

    public void printStructure(String indent) {
        System.out.printf("%s%s（￥%.2f）\n", indent, name, GetPrice());
        for (Component child : children) {
            if (child instanceof Composite) {
                ((Composite) child).printStructure(indent + "  ");
            } else if (child instanceof Part) {
                System.out.printf("%s  %s（￥%.2f）\n", indent, child.GetName(), child.GetPrice());
            }
        }
    }
}


public class Test {
    public static void main(String[] args) {
        // 创建叶子节点（零件）
        Part screw = new Part("螺丝", 0.15);
        Part panel = new Part("面板", 12.99);
        Part shell = new Part("外壳", 25.99);

        // 创建组合件
        Composite core = new Composite("核心模组");
        core.add(panel);
        core.add(screw);

        Composite product = new Composite("整机");
        product.add(core);
        product.add(shell);

        // 输出结构信息与价格
        product.printStructure("");
        System.out.printf("整机总价：￥%.2f\n", product.GetPrice());
    }

}
